package com.dat255.tentatime;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;
/**
 * This class handles the user state, the interaction time methods are
 * not implemented in our version but we want to leave room for
 * future possibilities
 * @author Daniel Kristoffersson
 *
 */
public class UserState extends Application {
	
	private boolean myState = false;
	private int lastInteractionTime;
	private Boolean isScreenOff = false;
	//private??
	private Thread inactive;

	public void onCreate() {
		super.onCreate();
		
		// start the thread to detect inactivity
		// This functions is not implemented in the system yet
		// it is meant for future functions.
		startUserInactivityDetectThread();
		// creating receive SCREEN_OFF and SCREEN_ON broadcast msgs from the device.
		new ScreenReceiver();
	}
	/**
	 * Checks the activity of the user, if inactive in a period of 15 seconds,
	 * logout the user
	 */
	public void startUserInactivityDetectThread() {

		inactive = new Thread() {
			public void run() {
				while (true) {
					try {
						// checks every 15sec for inactivity
						sleep(15000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					if (getLastInteractionTime() > 120000 || isScreenOff) {
						// user has been inactive over a period of time
						// logout
						logoutUser();
					}
				}
			}

		};

		inactive.start();
	}
	/**
	 * Get the latest interaction time of the user
	 * @return latest interaction time
	 */
	public long getLastInteractionTime() {
		return lastInteractionTime;
	}
	/**
	 * Set the latest interaction time
	 * @param lastInteraction set new interaction time
	 */
	public void setLastInteractionTime(int lastInteraction) {
		lastInteractionTime = lastInteraction;
	}
	/**
	 * This class checks if the screen is off/on (locked/unlocked)
	 */
	private class ScreenReceiver extends BroadcastReceiver {

		protected ScreenReceiver() {
			// register receiver that handles screen on and screen off logic
			IntentFilter filter = new IntentFilter();
			filter.addAction(Intent.ACTION_SCREEN_ON);
			filter.addAction(Intent.ACTION_SCREEN_OFF);
			registerReceiver(this, filter);
		}

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
				isScreenOff = true;
			} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
				isScreenOff = false;
			}
		}
	}
	/**
	 * Get user state
	 * @return true or false
	 */
	public boolean getState() {
		return myState;
	}
	/**
	 * Set user state to either false or true
	 * @param s either true o false
	 */
	public void setState(boolean s) {
		myState = s;
		if(s) {
		}
	}
	/**
	 * Logout user
	 * @return true if successful
	 */
	public boolean logoutUser() {
		if(myState) {
			setState(false);
			Toast.makeText(this, "You have been logged out!", Toast.LENGTH_SHORT).show();
			return true;
		}
		return false;
	}
	/**
	 * Login user
	 * @return true if successful
	 */
	public boolean loginUser() {
		if(!myState) {
			setState(true);
			Toast.makeText(this, "Login successful.", Toast.LENGTH_LONG).show();
			return true;
		}
		return false;
	}
	
	///////////////    TESTS     //////////////////
	
	/**
	 * This method is for testing only
	 * @return true if successful
	 */
	public boolean testLogoutUser() {
		if(myState) {
			setState(false);
			return true;
		}
		return false;
	}
	/**
	 * This method is for testing only
	 * @return true if successful
	 */
	public boolean testLoginUser() {
		if(!myState) {
			setState(true);
			return true;
		}
		return false;
	}
	/**
	 * This method is meant only for testing
	 * @return true if screen turned off else false
	 */
	public boolean setScreenOff() {
		if(!isScreenOff) {
			isScreenOff = true;
			return true;
		}
		return false;
	}
	/**
	 * This mehod is meant only for testing
	 * @return true of screen turned on else false
	 */
	public boolean setScreenOn() {
		if(isScreenOff) {
			isScreenOff = false;
			return true;
		}
		return false;
	}
}